package com.example.yichuangyun.mvvm.ui.activity;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityMapNavigationBinding;
import com.example.yichuangyun.dialog.ChoosePhotoDialog;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.MapUtil;
import com.example.yichuangyun.utils.ToastHelper;

/**
 * 地图导航（暂无用）
 */
public class MapNavigationActivity extends BaseActivity<ActivityMapNavigationBinding, BaseViewModel> {

    private TextView tvNav;
    private MapView mapView;
    private AMap mAMap;
    private Double mLatitude, mLongitude, cLatitude, cLongitude;
    private String address;
    private boolean isNeedLocation = true;

    private ChoosePhotoDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_navigation);
        CommonUtil.requestTranslucentStatusBar(this, true);
        initTitleBar("公司地址");
        initDialog();
        TextView tvCompanyName = findViewById(R.id.tv_company_name);
        TextView tvAddress = findViewById(R.id.tv_address);
        tvNav = findViewById(R.id.tv_nav);
        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        init();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String companyName = bundle.getString(Constant.nameKey);
            cLongitude = bundle.getDouble(Constant.longitudeKey);
            cLatitude = bundle.getDouble(Constant.latitudeKey);
            address = bundle.getString(Constant.addressKey);
            tvCompanyName.setText(companyName);
            tvAddress.setText(address);
        }
        initListener();
    }

    private void init() {
        if (mAMap == null) {
            // 初始化地图
            mAMap = mapView.getMap();
            // 定位蓝点
            MyLocationStyle myLocationStyle = new MyLocationStyle();
            myLocationStyle.showMyLocation(true);
            myLocationStyle.radiusFillColor(Color.TRANSPARENT);
            myLocationStyle.strokeWidth(0);
            myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_location_now)));
            myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//定位一次，且将视角移动到地图中心点。
            mAMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
            mAMap.getUiSettings().setMyLocationButtonEnabled(false); //设置默认定位按钮是否显示，非必需设置。
            mAMap.setMyLocationEnabled(true); // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
            mAMap.setOnMyLocationChangeListener(location -> {
                if (isNeedLocation) {
                    isNeedLocation = false;
                    mLatitude = location.getLatitude();
                    mLongitude = location.getLongitude();
                    if (mLatitude == 0 && mLongitude == 0) {
                        ToastHelper.show("请检查是否开启定位权限");
                        return;
                    }
                    mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLatitude, mLongitude), 18));//设置地图放大为14倍
                }
            });
        }
    }

    private void initListener() {
        tvNav.setOnClickListener(view -> {
            dialog.showDialog();
        });
    }

    private void initDialog() {
        String title1 = "高德地图" + (!MapUtil.isGdMapInstalled(context) ? "（安装）" : "");
        String title2 = "百度地图" + (!MapUtil.isBaiduMapInstalled(context) ? "（安装）" : "");
        dialog = new ChoosePhotoDialog(this, title1, title2) {
            @Override
            public void chooseOne() {
                MapUtil.goToGaoDeMap(context, address, mLatitude, mLongitude, cLatitude, cLongitude);
            }

            @Override
            public void chooseTwo() {
                MapUtil.goToBaiduMap(context, address, mLatitude, mLongitude, cLatitude, cLongitude);
            }
        };
    }

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {

    }
}